﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Joyintech.Common;
using Joyintech.Model.inpiao365;
using Joyintech.BLL.inpiao365;

namespace Joyintech.Admin.inpiao365.general.system.area
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //绑定所有一级区划
                DataBindArea();

                //绑定用户列表
                DataBindSystemUser();
            }
        }

        #region 绑定所有一级区划

        /// <summary>
        /// 绑定所有一级区划
        /// </summary>
        private void DataBindArea()
        {
            this.ddlProvince.Items.Clear();

            this.ddlProvince.Items.Add(new ListItem("--所有区域--", "0"));

            List<systemArea> list = new List<systemArea>();

            list = new systemArea_BLL().GetModelList("preencode is null");

            foreach (systemArea model in list)
            {
                this.ddlProvince.Items.Add(new ListItem(model.Name, model.Encode.ToString()));
            }
        }

        /// <summary>
        /// 递归绑定下级部门
        /// </summary>
        /// <param name="menuPID"></param>
        private void DataBindAreaChild(string encode, string frontStr)
        {
            List<systemArea> list = new List<systemArea>();

            if (DNTRequest.GetQueryString("id") != "")
            {
                list = new systemArea_BLL().GetModelList("preencode='" + encode + "' and id<>'" + DNTRequest.GetQueryString("id") + "'");
            }
            else
            {
                list = new systemArea_BLL().GetModelList("preencode='" + encode + "'");
            }

            foreach (systemArea model in list)
            {
                this.ddlCity.Items.Add(new ListItem(frontStr + model.Name, model.Encode.ToString()));

                //递归绑定下级部门
                DataBindAreaChild(model.Encode, "--" + frontStr);
            }
        }

        #endregion

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            DataBindSystemUser();
        }

        private void DataBindSystemUser()
        {
            StringBuilder strSql = new StringBuilder();

            if (this.txtName.Text.Trim() != "")
            {
                strSql.Append(" and name like '%" + this.txtName.Text.Trim() + "%'");
            }

            if (this.ddlCity.SelectedValue == "0")
            {
                if (this.ddlProvince.SelectedValue != "0")
                {
                    strSql.Append(" and preencode='" + this.ddlProvince.SelectedValue + "'");
                }
            }
            else
            {
                strSql.Append(" and preencode='" + this.ddlCity.SelectedValue + "'");
            }            

            this.SqlPager1.CurrentPageIndex = 0;
            this.SqlPager1.ConnStringKeyName = "localServer";
            this.SqlPager1.SelectCommand = "*";
            this.SqlPager1.SelectCommandTable = "systemArea";
            this.SqlPager1.SelectCommandWhere = "1=1" + strSql.ToString();
            this.SqlPager1.SortField = "encode";
            this.SqlPager1.SortStyle = WebControls.SortMode.ASC;
            this.SqlPager1.DataBind();
        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ddlCity.Items.Clear();

            DataBindAreaChild(this.ddlProvince.SelectedValue, "");

            this.ddlCity.Items.Insert(0,new ListItem("--不限--", "0"));
        }

    }
}